package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.handler.MessageContext;
import java.util.List;

/**
 * @descriptions: ...
 * @author: 唐杰
 * @date: 2019/11/20  15:25
 * @version: 1.0
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;


    /*
    * 根据id查询单条数据
    * */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,"查询权限数据成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限数据失败");
        }
    }

    /*
    根据条件分页查询所有权限数据*/
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findPage(queryPageBean);
    }

    /*
    * 查询所有权限数据
    */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Permission> permissionList = permissionService.findAll();
            //查询成功
            return new Result(true,"查询权限数据成功",permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false,"查询权限数据失败");
        }
    }

    /*
    新增权限数据*/
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try{
            permissionService.add(permission);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /*
    编辑权限数据*/
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            //编辑成功
            return new Result(true,"编辑权限数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            //编辑失败
            return new Result(false,"编辑权限数据失败");
        }
    }

    /*
    *根据id删除权限数据
    * */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            //删除权限数据成功
            return new Result(true,"删除权限数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            //删除权限数据失败
            return new Result(false,"删除权限数据失败");
        }
    }
}
